package com.lk.server.expose;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.lk.entity.Service;
import com.lk.server.config.NamicServiceConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import static cn.hutool.core.net.NetUtil.getLocalhostStr;

/**
 * 这个类的作用: 注册到注册中心
 * 命名服务
 *
 * @author kun.li
 */
@Component
@Slf4j
public class ExposeService implements ApplicationListener<ContextRefreshedEvent> {


    @Autowired
    private NamicServiceConfig namicServiceConfig;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 组装请求对象
        Service service = new Service();
        service.setServiceName(namicServiceConfig.getServiceName());
        service.setPort(8585);
        service.setIp(getLocalhostStr());
        // 发送
        String post = HttpUtil.post(namicServiceConfig.getAddress() + "/register", JSONUtil.toJsonStr(service));
        log.info("注册成功:{}", post);

    }
}
